import pymysql
import traceback
class DB:
    def __init__(self,user,password,host,database):
        self.user = user
        self.password = password
        self.host = host
        self.database = database
    def connect(self):
        self.db = pymysql.connect(user =self.user,password=self.password,host=self.host,database=self.database,cursorclass=pymysql.cursors.DictCursor)
        self.cursor = self.db.cursor()
    def close(self):
        self.cursor.close()
        self.db.close()
    def search(self,type:int,sql):
        self.connect()
        return = None
        if type == 1:
            try:
                self.cursor.execute(sql)
                return result
            except:
                self.db.rollback()
                traceback.print_exc()
                return result
        elif type ==2:
            try:
                self.cursor.execute(sql)
                result = self.cursor.fetchall()
                self.close()
                return result
            except:
                self.db.rollback()
                traceback.print_exc()
                return result
    def __edit(self,sql):
        self.connect()
        try:
            self.cursor.execute(sql)
            self.db.commit()
            self.close()
        except:
            self.db.rollback()
